package shooter.maps;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

/**
 * This is the image bar of the Mapeditor.
 * 
 * @author Michail Tausch
 */
public class ImageBar extends JPanel{

	private static final long serialVersionUID = 1L;

	private BufferedImage[] images;
	
	private int selectedSprite;
	
	/**
	 * Constructor receives an array of images an creates a image bar.
	 * @param images
	 */
	public ImageBar(BufferedImage[] images) {
		super();
		this.images = images;
		this.selectedSprite = 0;
		this.setPreferredSize(new Dimension(images[0].getWidth(), images.length * images[0].getHeight()));
	}
	
	/**
	 * Paints the image bar.
	 */
	public void paint(Graphics g){
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		for(int i = 0; i < images.length; i++){
			g.drawImage(images[i], 0, i * images[0].getHeight(), null);
		}
		
		g.setColor(Color.GREEN);
		g.drawRect(0, selectedSprite * images[0].getHeight(), images[0].getWidth(), images[0].getHeight());
	}
	
	/**
	 * Returns the number of the selected sprite.
	 * @param x
	 * @param y
	 * @return
	 */
	public int getSpriteNumber(int x, int y){
		int tempSelectedSprite = y / images[0].getHeight();
		
		if(tempSelectedSprite < images.length){
			selectedSprite = tempSelectedSprite;
			repaint();
			return selectedSprite;
		} else{
			throw new IllegalArgumentException("For x/y values no sprite is available.");
		}
	}
}
